package com.wzy.level.category.categoryServlet;

import com.sun.org.apache.xerces.internal.impl.xpath.regex.ParseException;
import com.wzy.Jdbc封装.pojo.News_category;
import com.wzy.Jdbc封装.pojo.News_detail;
import com.wzy.level.category.categoryService.categoryService;
import com.wzy.level.category.categoryServiceImpl.categoryServiceImpl;
import com.wzy.level.detail.detailService.detailService;
import com.wzy.level.detail.detailServiceImpl.detailServiceImpl;
import com.wzy.level.detail.detailServlet.detail;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

@WebServlet("/category_form_mid")
public class category_form_insert_update extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

//        req.setCharacterEncoding("utf-8");
//    int id = Integer.parseInt(request.getParameter("id"));
//    int categoryId = Integer.parseInt(request.getParameter("categoryId").trim());
//    String title = request.getParameter("title");
//    String summary = request.getParameter("summary");
//    String content = request.getParameter("content");
        int id =0;
        String name = null;
        String screateDate =null;


        int state =0;

        boolean isMultipartFile = ServletFileUpload.isMultipartContent(req);
        if(isMultipartFile){
            FileItemFactory fileItemFactory = new DiskFileItemFactory();
            ServletFileUpload servletFileUpload = new ServletFileUpload(fileItemFactory);

            List<FileItem> list = null;
            try {
                list = servletFileUpload.parseRequest(req);
            } catch (FileUploadException e) {
                e.printStackTrace();
            }
            Iterator<FileItem> iterator = list.iterator();
            while(iterator.hasNext()){
                FileItem item = iterator.next();
                boolean formField = item.isFormField();
                if(formField){
                    String fieldName = item.getFieldName();
                    if(fieldName.equals("id")){
                        id = Integer.parseInt(item.getString());
                    }else if(fieldName.equals("name")){
//            title = item.getString();
                        name = new String((item.getString("iso8859-1")).getBytes("iso8859-1"),"utf-8");
                    }else if(fieldName.equals("date")){
                        screateDate = item.getString().trim();
                    }else if(fieldName.equals("state")){
                        state = Integer.parseInt(item.getString());
                    }
                }
            }
        }

        //上传结束

        Date createDate = null;

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-mm-dd");
        try {

            createDate = simpleDateFormat.parse(screateDate);

            simpleDateFormat.setLenient(false);
        }catch (ParseException | java.text.ParseException e){
            e.printStackTrace();
        }

        News_category news_category = new News_category();


//    URLEncoder.encode(picPath,"utf-8");
//    new String(picPath.getBytes(StandardCharsets.UTF_8));

//    title.getBytes(StandardCharsets.UTF_8);


        news_category.setName(name);

        news_category.setDate(createDate);




//    String sstate = request.getParameter("state");
//    int state = Integer.parseInt(sstate);

        categoryService category = new categoryServiceImpl();

        if(state==0){
            news_category.setId(id);
            int update = category.update(news_category);
            req.setAttribute("update",update);
            if(update>0){
//                req.getRequestDispatcher("/pageManage?cate=1").forward(req,resp);
                resp.sendRedirect("http://localhost:8080//pageManage?cate=4");
            }else {
                System.out.println("修改失败");
            }
        }else if(state == 1){
            int insert = category.insert(news_category);
            req.setAttribute("insert",insert);
            if(insert>0){
                resp.sendRedirect("http://localhost:8080//pageManage?cate=4");
            }else {
                System.out.println("新增失败");
            }
        }
    }
}
